package com.monitor.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.monitor.dao.UserDao;
import com.monitor.po.User;
import com.monitor.util.Pager;

@SuppressWarnings("unchecked")
public class UserDaoImpl implements UserDao {
	private SessionFactory sessionFactory;

	/**
	 * 根据电话号码获取用户
	 */
	@Override
	public User getUserByPhone(String phone) throws Exception {
		try {
			List<User> userList = sessionFactory.getCurrentSession().createQuery("from User u where u.phone=:phone").setString("phone", phone).list();
			if (userList != null && !userList.isEmpty()) {
				return userList.get(0);
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getUserByPhone()....");
		}
	}

	@Override
	public Pager<User> getAllUser(int pageSize, int currentPage) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from User u where u.visible=:visible").setBoolean("visible", true);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
			List<User> list = sesion.createQuery("from User u where u.visible=:visible order by u.id desc").setBoolean("visible", true).setFirstResult((currentPage - 1) * pageSize)
					.setMaxResults(pageSize).list();
			Pager<User> pager = new Pager<User>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getAllUser()....");
		}

	}

	@Override
	public Pager<User> getUserByVipType(boolean isVip, int pageSize, int currentPage) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from User u where u.vip=:vip and u.visible=:visible").setBoolean("vip", isVip).setBoolean("visible", true);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
			List<User> list = sesion.createQuery("from User u where u.vip=:vip and u.visible=:visible order by u.id desc").setBoolean("vip", isVip).setBoolean("visible", true)
					.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
			Pager<User> pager = new Pager<User>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getUserByVipType()....");
		}
	}

	@Override
	public void addUser(User user) throws Exception {
		try {
			sessionFactory.getCurrentSession().persist(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting addUser()....");
		}
	}

	@Override
	public void updateUser(User user) throws Exception {
		try {
			sessionFactory.getCurrentSession().update(user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting updateUser()....");
		}
	}

	@Override
	public User getUserById(Long id) throws Exception {
		try {
			return (User) sessionFactory.getCurrentSession().get(User.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getUserById()....");
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
